package band.wukong.practice.springcloud.quickstart.qk09;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author wukong(wukonggg@139.com)
 */
@RestController
@SpringBootApplication
public class Qk09HiServiceApplication {

    private static final Logger logger = LoggerFactory.getLogger(Qk09HiServiceApplication.class);

    @Value("${spring.application.name}")
    private String appName;

    @Value("${app.serviceDirectory.qk09.nihaoService.replyEndpoint.url}")
    private String nihaoServiceReplyEndpointUrl;

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    @RequestMapping("/reply")
    public String reply(){
        logger.info(appName + " is being called.");
        return "I'm " + appName;
    }

    @RequestMapping("/hi")
    public String hi(){
        logger.info("calling trace " + appName);
        return restTemplate.getForObject(nihaoServiceReplyEndpointUrl, String.class);
    }

    @Bean
    public AlwaysSampler defaultSampler(){
        return new AlwaysSampler();
    }

    public static void main(String[] args) {
        SpringApplication.run(Qk09HiServiceApplication.class, args);
    }

}
